package com.nativetalk.service.four.teacher;

import com.nativetalk.base.Page;
import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.dict.TeacherLanguage;
import com.nativetalk.bean.dict.TsNtDictDeatil;
import com.nativetalk.bean.teacher.TdNtTeacherList;
import com.nativetalk.bean.teacher.TdNtTeacherListCourse;
import com.nativetalk.bean.teacher.TdNtTeacherResult;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.MemcachedKey;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.dao.TdNtTeacherDao;
import com.nativetalk.dao.TdNtTeacherLabelDao;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 2016/10/31.
 * Modify by wangzheng on 2017/2/9.
 */
@Service
public class FourTeacherServiceImpl implements FourTeacherService {
    private static Logger logger = Logger.getLogger(FourTeacherServiceImpl.class);
    @Resource
    private TdNtTeacherDao tdNtTeacherDao;

    @Resource
    private TdNtTeacherLabelDao tdNtTeacherLabelDao;

    @Override
    public RetInfo findTeacherByCourse(Page page) {
        String logInfo = this.getClass().getName() + ":index:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Map<String, TsNtDictDeatil> tsNtDictDeatilsMap =
                    (Map<String, TsNtDictDeatil>) MemcachedUtils.get(MemcachedKey.tsNtDictDeatilsMap);

            page.setPageSize(9);
            /**
             * 教师信息:id,头像,状态,名称,学校,标签
             */
            Map<String, Object> map = new HashMap<>();
            int currentPage = Integer.valueOf(page.getCurrentPage());
            int pageSize = page.getPageSize();//每页数据
            int totalPage = 0;//总页数
            int totalRecord = tdNtTeacherDao.findTeacherByCateSize(page);//总条数
            List<TdNtTeacherListCourse> tdNtTeacherLists = null;
            if (totalRecord > 0) {
                if (totalRecord % pageSize == 0) {
                    totalPage = totalRecord / pageSize;
                } else {
                    totalPage = (totalRecord / pageSize) + 1;
                }
                if (currentPage >= totalPage) {
                    currentPage = totalPage;
                }
                if (currentPage <= 1) {
                    currentPage = 1;
                }
                page.setCurrentPage((currentPage - 1) * pageSize);
                tdNtTeacherLists = tdNtTeacherDao.findTeacherByCatePage(page);
                for (int i = 0; i < tdNtTeacherLists.size(); i++) {
                    if (tdNtTeacherLists.get(i).getDict_id() != null) {
                        TsNtDictDeatil tsNtDictDeatil = tsNtDictDeatilsMap.get(tdNtTeacherLists.get(i).getDict_id());
                        tdNtTeacherLists.get(i).setDict_id(tsNtDictDeatil.getCode_name());
                    } else {
                        TsNtDictDeatil tsNtDictDeatil = tsNtDictDeatilsMap.get("3");
                        tdNtTeacherLists.get(i).setDict_id(tsNtDictDeatil.getCode_name());
                    }
                    String country = tdNtTeacherLists.get(i).getNationality();
                    if (country.equals("0") || country.equals("315")) {
                        country = "1";
                    }
                    String nationality = tsNtDictDeatilsMap.get(country).getCode_name();
                    tdNtTeacherLists.get(i).setNationality(nationality);

                }
            }
            map.put("teachers", tdNtTeacherLists);
            map.put("tea_totalPage", totalPage);
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取数据成功");
            retInfo.setObj(map);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(), e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo findTeacherIndexList(Page page) {
        String logInfo = this.getClass().getName() + ":findTeacherIndexList:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();

        try {
            int currentPage = Integer.valueOf(page.getCurrentPage());
            int pageSize = page.getPageSize();//每页数据
            int totalPage;//总页数
          //  int totalRecord = tdNtTeacherDao.findTdNtTeacherSize(page);//总条数
            int totalRecord = tdNtTeacherDao.selectTdNtTeacherSize(page);//总条数
            if (totalRecord > 0) {
                if (totalRecord % pageSize == 0) {
                    totalPage = totalRecord / pageSize;
                } else {
                    totalPage = (totalRecord / pageSize) + 1;
                }
                if (currentPage >= totalPage) {
                    currentPage = totalPage;
                }
                if (currentPage <= 1) {
                    currentPage = 1;
                }
                page.setCurrentPage((currentPage - 1) * pageSize);
                page.setObj(0);
                Map<String, TsNtDictDeatil> tsNtDictDeatilsMap = (Map<String, TsNtDictDeatil>) MemcachedUtils.get(MemcachedKey.tsNtDictDeatilsMap);

                List<TdNtTeacherList> tdNtTeacherLists = tdNtTeacherDao.selectTdNtTeacherList(page);
                for (int i = 0; i < tdNtTeacherLists.size(); i++) {
                    if (tdNtTeacherLists.get(i).getDict_id() != null) {
                        TsNtDictDeatil tsNtDictDeatil = tsNtDictDeatilsMap.get(tdNtTeacherLists.get(i).getDict_id());
                        tdNtTeacherLists.get(i).setDict_id(tsNtDictDeatil.getCode_name());
                    } else {
                        TsNtDictDeatil tsNtDictDeatil = tsNtDictDeatilsMap.get("3");
                        tdNtTeacherLists.get(i).setDict_id(tsNtDictDeatil.getCode_name());
                    }
                    String country = tdNtTeacherLists.get(i).getNationality();
                    if (country.equals("0") || country.equals("315")) {
                        country = "1";
                    }
                    String nationality = tsNtDictDeatilsMap.get(country).getCode_name();
                    tdNtTeacherLists.get(i).setNationality(nationality);
                    /**
                     * 查找老师粉丝数
                     */
                    int funsnumber = tdNtTeacherDao.findFunsSize(tdNtTeacherLists.get(i).getTea_id());
                    tdNtTeacherLists.get(i).setFans_size(funsnumber);

                    String collection_flag = "1";

                    tdNtTeacherLists.get(i).setCollection_flag(collection_flag);
                    /**
                     * 查找老师系统标签
                     */
                    Map<String, Object> params = new HashMap<>();
                    params.put("tea_id", tdNtTeacherLists.get(i).getTea_id());
                    params.put("lab_type", 1);
                    tdNtTeacherLists.get(i).setLabels(tdNtTeacherLabelDao.findTdNtTeacherListTeaIdAndType(params));
                }
                if (tdNtTeacherLists == null) {
                    tdNtTeacherLists = new ArrayList<>();
                }
                Map<String, Object> obj = new HashMap<String, Object>();
                obj.put("totalPage", totalPage);
                obj.put("teachers", tdNtTeacherLists);
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("获取首页老师数据成功");
                retInfo.setObj(obj);
            } else {
                Map<String, Object> obj = new HashMap<String, Object>();
                obj.put("totalPage", 0);
                obj.put("teachers", new ArrayList<>());
                retInfo.setObj(obj);
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("暂无老师");
            }

        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(), e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo findTeacherCourseList(Page page) {
        String logInfo = this.getClass().getName() + ":findTeacherCourseList:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Map<String, TsNtDictDeatil> tsNtDictDeatilsMap = (Map<String, TsNtDictDeatil>) MemcachedUtils.get(MemcachedKey.tsNtDictDeatilsMap);

            /**
             * 教师信息:id,头像,状态,名称,学校,教学时长
             */
            Map<String, Object> map = new HashMap<>();
            int currentPage = Integer.valueOf(page.getCurrentPage());
            int pageSize = page.getPageSize();//每页数据
            int totalPage = 0;//总页数
            int totalRecord = tdNtTeacherDao.findTeacherByCourseSize(page);//总条数
            List<TdNtTeacherListCourse> tdNtTeacherLists = null;
            if (totalRecord > 0) {
                if (totalRecord % pageSize == 0) {
                    totalPage = totalRecord / pageSize;
                } else {
                    totalPage = (totalRecord / pageSize) + 1;
                }
                if (currentPage >= totalPage) {
                    currentPage = totalPage;
                }
                if (currentPage <= 1) {
                    currentPage = 1;
                }
                page.setCurrentPage((currentPage - 1) * pageSize);
                /**
                 * SELECT teacher.tea_id,teacher.tea_head_portrait,teacher.state,teacher.dict_id,teacher.tea_name,teacher.nationality,teacher.tea_total_times
                 * from td_nt_teacher teacher,td_nt_teach_course teach
                 * WHERE teach.cou_ID=#{obj} and teacher.TEA_ID=teach.TEA_ID and teacher.test=1 and teacher.tea_islock=1 and teach.TC_STATE = '2'
                 */
                tdNtTeacherLists = tdNtTeacherDao.findTeacherByCoursePage(page);
                for (int i = 0; i < tdNtTeacherLists.size(); i++) {
                    if (tdNtTeacherLists.get(i).getDict_id() != null) {
                        TsNtDictDeatil tsNtDictDeatil = tsNtDictDeatilsMap.get(tdNtTeacherLists.get(i).getDict_id());
                        tdNtTeacherLists.get(i).setDict_id(tsNtDictDeatil.getCode_name());
                    } else {
                        TsNtDictDeatil tsNtDictDeatil = tsNtDictDeatilsMap.get("3");
                        tdNtTeacherLists.get(i).setDict_id(tsNtDictDeatil.getCode_name());
                    }
                    String country = tdNtTeacherLists.get(i).getNationality();
                    if (country.equals("0") || country.equals("315")) {
                        country = "1";
                    }
                    String nationality = tsNtDictDeatilsMap.get(country).getCode_name();
                    tdNtTeacherLists.get(i).setNationality(nationality);
                }
            }
            map.put("teachers", tdNtTeacherLists);
            map.put("tea_totalPage", totalPage);
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取数据成功");
            retInfo.setObj(map);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(), e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo updateTeacherContact(TdNtTeacherResult tdNtTeacherResult) {
        String logInfo = this.getClass().getName() + ":updateTeacherContact:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            tdNtTeacherDao.updateTeacherContact(tdNtTeacherResult);
            MemcachedUtils.replace(tdNtTeacherResult.getToken(), tdNtTeacherResult, 20 * 24 * 60 * 60);
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("更新成功");
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(), e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    // 查询所有语种
    public List<TeacherLanguage> findTeacherLanguage() {
        Object o = MemcachedUtils.get(MemcachedKey.language);
        List<TeacherLanguage> list = new ArrayList<>();
        try {
            if (o != null && List.class.isInstance(o)) {
                list = (List<TeacherLanguage>) o;
            } else {
                logger.info("TeacherLanguage 缓存未命中");
                list = tdNtTeacherDao.findTeacherLanguages(MemcachedKey.language);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return list;
    }
}
